package com.ea.admin.controller.tutelage;

import com.ea.admin.pojo.param.position.GetPositionListParam;
import com.ea.admin.pojo.param.tutelage.ExportWarningInfoParam;
import com.ea.admin.pojo.vo.position.GetPositionListVo;
import com.ea.admin.service.WarningService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.auth.NowUser;
import com.ea.common.log.LogAnnotation;
import com.ea.common.report.pojo.param.GetPropertiesListParam;
import com.ea.common.report.pojo.param.GetWarningInfoParam;
import com.ea.common.report.pojo.vo.GetPropertiesListVo;
import com.ea.common.report.pojo.vo.GetWarningInfoVo;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@Api(tags = "监护管理-预警管理")
@RequestMapping("/admin/warning")
public class WarningController {
    @Resource
    WarningService warningService;
    @Resource
    NowUserGetter nowUserGetter;
    @Resource
    HttpServletResponse response;

    @Transactional
    @Operation(summary = "获得预警人数信息")
    @PostMapping("/getWarningInfoVo")
    @ApiImplicitParam(dataType = "GetWarningInfoParam", name = "getWarningInfoParam", value = "获得预警人数信息", readOnly = true, required = true)
    @LogAnnotation(module = "WarningController", operation = "获得预警人数信息")
    public Result<GetWarningInfoVo> getWarningInfoVo(@RequestBody @Valid GetWarningInfoParam getWarningInfoParam) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        GetWarningInfoVo result = warningService.getWarningInfoVo(
                nowUser.getAccountId(),
                ParamUtil.parseIdList(getWarningInfoParam.getCompanyIdList()),
                ParamUtil.parseIdList(getWarningInfoParam.getDepartmentIdList()),
                ParamUtil.parseIdList(getWarningInfoParam.getPositionIdList()),
                ParamUtil.parseIdList(getWarningInfoParam.getPropertiesIdList()),
                getWarningInfoParam.getName(),
                getWarningInfoParam.getWarningTypeList(),
                getWarningInfoParam.getPage(),
                getWarningInfoParam.getPageSize()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "导出预警人数信息")
    @PostMapping("/exportWarningInfoVo")
    @ApiImplicitParam(dataType = "ExportWarningInfoParam", name = "exportWarningInfoParam", value = "导出预警人数信息", readOnly = true, required = true)
    @LogAnnotation(module = "WarningController", operation = "导出预警人数信息")
    public void exportWarningInfoVo(@RequestBody @Valid ExportWarningInfoParam exportWarningInfoParam) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        warningService.exportWarningInfoVo(
                nowUser.getAccountId(),
                exportWarningInfoParam.getWarningType(),
                response
        );
    }

    @Transactional
    @Operation(summary = "获取所有预警指标")
    @PostMapping("getPropertiesList")
    @LogAnnotation(module = "WarningController", operation = "获取所有预警指标")
    public Result<GetPropertiesListVo> getPropertiesList(){
        GetPropertiesListVo result = warningService.getPropertiesList();
        return Result.success(result);
    }

}
